package com.atguigu.tingshu.user.api;

import com.atguigu.tingshu.common.login.RequiresLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.user.service.UserListenProcessService;
import com.atguigu.tingshu.vo.user.UserListenProcessVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Map;

@Tag(name = "用户声音播放进度管理接口")
@RestController
@RequestMapping("api/user")
public class UserListenProcessApiController {

    private final UserListenProcessService userListenProcessService;

    public UserListenProcessApiController(UserListenProcessService userListenProcessService) {
        this.userListenProcessService = userListenProcessService;
    }

    // /api/user/userListenProcess/getTrackBreakSecond/{trackId}
    @Operation(summary = "获取当前用户的声音的上次跳出时间")
    @Parameters({
            @Parameter(name = "trackId", description = "声音ID", required = true)
    })
    @RequiresLogin(required = false)
    @GetMapping("/userListenProcess/getTrackBreakSecond/{trackId}")
    public Result<BigDecimal> getTrackBreakSecond(@PathVariable Long trackId) {
        Long userId = AuthContextHolder.getUserId();
        if (userId == null) {
            return Result.ok();
        }
        BigDecimal trackBreakSecond = userListenProcessService.getTrackBreakSecondByTrackId(trackId, userId);
        return Result.ok(trackBreakSecond);
    }

    // /api/user/userListenProcess/updateListenProcess
    @Operation(summary = "更新用户当前播放声音进度")
    @RequiresLogin(required = false)
    @PostMapping("/userListenProcess/updateListenProcess")
    public Result<Void> updateListenProcess(@RequestBody UserListenProcessVo userListenProcessVo) {
        Long userId = AuthContextHolder.getUserId();
        if (userId != null) {
            this.userListenProcessService.updateListenProcess(userId, userListenProcessVo);
        }
        return Result.ok();
    }

    // /api/user/userListenProcess/getLatelyTrack
    @Operation(summary = "获取用户最近一次播放记录")
    @RequiresLogin
    @GetMapping("/userListenProcess/getLatelyTrack")
    public Result<Map<String, Long>> getLatelyTrack() {
        Long userId = AuthContextHolder.getUserId();
        return Result.ok(this.userListenProcessService.getLatelyTrack(userId));
    }
}

